//
//  CZQStudentExeViewController.m
//  coachmanage
//
//  Created by 众拳科技 on 2018/3/20.
//  Copyright © 2018年 众拳科技. All rights reserved.
//

#import "CZQStudentExeViewController.h"
#import "CZQStudentExeView.h"
#import "CZQLongViewController.h"
@interface CZQStudentExeViewController ()
{
    
}

/**
 存放擅长数据
 */
@property(nonatomic,strong)NSMutableArray *longArrM;
/**
 存放健身后照片数据
 */
@property(nonatomic,strong)NSMutableArray *sportBeforArrM;

/**
 存放健身前照片数据
 */
@property(nonatomic,strong)NSMutableArray *sportAfterArrM;

/**
 头像url
 */
@property(nonatomic,strong)NSString *iconUrl;

/**
 学员案例View
 */
@property(weak,nonatomic)CZQStudentExeView *studentExeView;

@end

@implementation CZQStudentExeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"学员案例";
    
    self.resumeModel = [CZQResumeModel new];
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(rightBtnClick)];
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateNormal];
    [rightButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:CSBlackColor,NSFontAttributeName:MidFont} forState:UIControlStateHighlighted];
    
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
    
    [self createSubView];
    
    
    
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    
    if (self.resumeModel.FavTags.length > 0) {
        
        self.studentExeView.longArrM = [self.resumeModel.FavTags componentsSeparatedByString:@","];
        
        self.studentExeView.model = self.resumeModel;
    }
    
   
    self.studentExeView.sportAfterArrM = self.sportAfterArrM;
    
    self.studentExeView.sportBeforArrM = self.sportBeforArrM;
    
    
}



#pragma mark - 懒加载
-(NSMutableArray *)longArrM
{
    if (!_longArrM) {
        _longArrM = [NSMutableArray array];
    }
    
    return _longArrM;
}
-(NSMutableArray *)sportAfterArrM
{
    if (!_sportAfterArrM) {
        _sportAfterArrM = [NSMutableArray array];
    }
    
    return _sportAfterArrM;
}
-(NSMutableArray *)sportBeforArrM
{
    if (!_sportBeforArrM) {
        _sportBeforArrM = [NSMutableArray array];
    }
    
    return _sportBeforArrM;
}

-(void)createSubView
{
    CZQStudentExeView *view = [CZQStudentExeView new];
    [self.view addSubview:view];
    self.studentExeView = view;
    
    [view mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view);
        
    }];
    
    
    
    //上传头像
    view.iconBtn.tag = 0;
    [view.iconBtn addTarget:self action:@selector(upIcon:)];
    
    //擅长跳转
    [view.labBtnView.choseBtn addTarget:self action:@selector(longSelect)];
    
    //自我感觉
    view.sportReportView.max = 30;
    view.sportReportView.min = 0;
    
    //健身前照片添加
    view.sportAfterBtn.tag = 1;
    [view.sportAfterBtn addTarget:self action:@selector(addSportPhoto:)];
    
    //健身后照片添加
    view.sportBefourBtn.tag = 2;
    [view.sportBefourBtn addTarget:self action:@selector(addSportPhoto:)];
    
    
    if (self.dicDat.count > 0) {
        view.deletBtn.hidden = NO;
        [view.deletBtn addTarget:self action:@selector(delet)];
        
        self.iconUrl = self.dicDat[@"Avatar"];
        [view.iconBtn sd_setImageWithURL:[Helper imageUrlStringToChinese:self.dicDat[@"Avatar"] imageType:nil] forState:UIControlStateNormal placeholderImage:PlaceHold];
        view.sportReportView.textView.text = self.dicDat[@"Description"];
        [view.sportReportView textViewDidChange:view.sportReportView.textView];
        
        NSString *after = self.dicDat[@"AfterPic"];
        NSString *befour = self.dicDat[@"BeforePic"];
        
        if (after.length > 0) {
            self.sportAfterArrM = [NSMutableArray arrayWithArray:[after componentsSeparatedByString:@","]];
            self.studentExeView.sportAfterArrM = self.sportAfterArrM;
        }
        
        if (befour.length > 0) {
            self.sportBeforArrM = [NSMutableArray arrayWithArray:[befour componentsSeparatedByString:@","]];
            self.studentExeView.sportBeforArrM = self.sportBeforArrM;
        }
        
        if (self.dicDat.count > 0) {
            
            NSString *tags = self.dicDat[@"Tag"];
            
            if (tags.length > 0) {
                
                
                
                self.resumeModel.FavTags = tags;
                
                self.studentExeView.longArrM = [self.resumeModel.FavTags componentsSeparatedByString:@","];
                
                self.studentExeView.model = self.resumeModel;
            }
            
            
        }
        
        
    }
    
    
}

#pragma mark 擅长跳转
-(void)longSelect
{
    CZQLongViewController *longVC = [CZQLongViewController new];
    
//    longVC.seletDataArrM = self.longArrM;
    
    
    
//    if (self.longArrM.count > 0) {
//        self.resumeModel.FavTags = [self.longArrM componentsJoinedByString:@","];
//    }else
//    {
//        self.resumeModel.FavTags = @"";
//    }
    
    longVC.resumeModel = self.resumeModel;
    
    [self.navigationController pushViewController:longVC animated:YES];
    
}

#pragma mark - 上传头像
-(void)upIcon:(UIButton *)sender
{
    [self logoSelect:sender];
    
}
#pragma mark - 上传健身前后
-(void)addSportPhoto:(UIButton *)sender
{
    [self logoSelect:sender];
    
}

#pragma Logo图片选择
-(void)logoSelect:(UIButton *)photoSelect{
    
    ZLPhotoPickerViewController *pickerVc = [[ZLPhotoPickerViewController alloc] init];
    
    if (photoSelect.tag == 0) {
        
    }
    
    pickerVc.maxCount = 9;
    
    // Jump AssetsVc
    pickerVc.status = PickerViewShowStatusCameraRoll;
    // Recoder Select Assets
    //    pickerVc.selectPickers = self.assets;
    // Filter: PickerPhotoStatusAllVideoAndPhotos, PickerPhotoStatusVideos, PickerPhotoStatusPhotos.
    
    pickerVc.photoStatus = PickerPhotoStatusPhotos;
    // Desc Show Photos, And Suppor Camera
    pickerVc.isShowCamera = YES;
    pickerVc.topShowPhotoPicker = YES;
    
    // CallBack
    __weak typeof(self)weakSelf = self;
    
    pickerVc.callBack = ^(NSArray<ZLPhotoAssets *> *status){
        
        for (ZLPhotoAssets *assets in status) {
            
            ZLPhotoPickerBrowserPhoto *photo = [ZLPhotoPickerBrowserPhoto photoAnyImageObjWith:assets];
            
            photo.asset = assets;
            

            UIImage *image = nil;
            
            //拍照
            if ([photo.asset isKindOfClass:[UIImage class]]) {
                
                image = (UIImage *)photo.asset;
            }
            else{
                image = [assets thumbImage];
                
            }
            
            if (photoSelect.tag == 0) {
                
                [photoSelect setImage:image forState:UIControlStateNormal];
                
            }else if (photoSelect.tag == 1){
                
//                [weakSelf.sportAfterArrM addObject:image];
                
            }else
            {
//                [weakSelf.sportBeforArrM addObject:image];
                
            }
            
            [weakSelf uploadImage:image type:photoSelect.tag];
            
//            break;
        }
        
        //跟新数据
        if (photoSelect.tag == 1) {
            
        }else if (photoSelect.tag == 2){
            
            
        }
    };
    
    [pickerVc showPickerVc:self];
}


#pragma mark 上传头像
-(void)uploadImage:(UIImage *)image type:(NSInteger )tag{
    
    
    
    NSData *imageData;
    
    
    WS(weakSelf);
    
    imageData  = UIImageJPEGRepresentation(image,0.1);
    
    [MBProgressHUD showMessage:@"上传中.." toView:self.view];
    
    RAYUploader *upload = [RAYUploader defaultUploader];
    
    [upload uploadWithData:imageData complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        
        [MBProgressHUD hideHUDForView:weakSelf.view];
        
        if (key) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSString *picUrl = [NSString stringWithFormat:@"%@%@",QiNiuBase,key];
                
                //跟新数据
                if (tag == 1) {
                    [self.sportAfterArrM addObject:picUrl]; 
                    self.studentExeView.sportAfterArrM = self.sportAfterArrM;
                }else if (tag == 2){
                    [self.sportBeforArrM addObject:picUrl];
                    self.studentExeView.sportBeforArrM = self.sportBeforArrM;
                }else
                {
                    
                    self.iconUrl = picUrl;
                }
            });
            
        }
        
    }];
    
}
#pragma mark - 删除学员案列
-(void)delet
{
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    
    
//    [params setValue:self.dicDat[@"ID"] forKey:@"ID"];@"http://open.zqtiyu.cn/api/MemberCase/DelMemberCase?ID=1"
    
    
    [HTTPTool POSTRaw:[NSString stringWithFormat:@"%@?ID=%@",Post_DelMemberCase,self.dicDat[@"ID"]] parameters:params
     
              success:^(id responseObject){
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                      
                      [MBProgressHUD showText:@"删除成功"];
                      
                      
                      [self.navigationController popViewControllerAnimated:YES];
                  }
                  
              }
     
              failure:^(NSError *error){
                  
                  [MBProgressHUD hideHUDForView:self.view];
              }];
    
}
#pragma mark - 添加学员案列
-(void)rightBtnClick
{
    [MBProgressHUD showMessage:@"请求中..." toView:self.view];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:_iconUrl forKey:@"Avatar"];
    
    [params setValue:self.resumeModel.FavTags forKey:@"Tag"];
    
    
    [params setValue:self.studentExeView.sportReportView.textView.text forKey:@"Description"];
    
    if (self.sportBeforArrM.count > 0) {
        [params setValue:[self.sportBeforArrM componentsJoinedByString:@","] forKey:@"BeforePic"];
    }
    
    
    if (self.sportAfterArrM.count > 0) {
        [params setValue:[self.sportAfterArrM componentsJoinedByString:@","] forKey:@"AfterPic"];
    }
    
    
    [params setObject:[[NSUserDefaults standardUserDefaults]objectForKey:@"UserID"] forKey:@"AddUserID"];
    
    if (self.dicDat.count > 0) {
        [params setValue:self.dicDat[@"ID"] forKey:@"ID"];
    }
    
    
    
    [HTTPTool POSTRaw:Post_MemberCaseOperate parameters:params
     
              success:^(id responseObject){
                  
                  [MBProgressHUD hideHUDForView:self.view];
                  
                  if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                      
                      [MBProgressHUD showText:@"添加成功"];
                      
                      
                      [self.navigationController popViewControllerAnimated:YES];
                  }
                  
              }
     
              failure:^(NSError *error){
                  
                  [MBProgressHUD hideHUDForView:self.view];
              }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
